<?php
namespace App\Controller;

use Think\Log;

class LoginController extends BaseController
{

    /**
     * 登录
     */
   public  function login()
    {
        $phone = I('phone');
        $password = I('password');

        //匹配手机号格式是否正确
        if (!preg_match('/^1[34578]\d{9}$/', $phone)) {
            $this->error(401, '手机号码格式不正确');
        }
        //匹配密码格式是否正确
        if (preg_match('/^\S{6,18}$/', $password)) {
            $data['password'] = sp_password($password);
        } else {
            $this->error(401, '密码格式不正确');
        }

        $user_info = M('member')->where("phone = '%s'", $phone)->find();

        if(!$user_info){
            $this->error(410,'未找到该账户，详细确认登录信息！');
        }

        if ($user_info && sp_compare_password($password,$user_info['password'])) {
            $device_token = I('device_token');
            if($device_token){
	            if(strlen($device_token)>44){
		           $save['phone_type']=2;
	            }else{
	            	$save['phone_type']=1;
	            }
	            $save['device_token']=$device_token;
            }
            $save['token'] = md5(sp_get_order_sn());
            $data = array(
                'uid' => $user_info['uid'],
                'name' => $user_info['name'],
                'token' => $save['token'],
                'device_token'=>$device_token
            );
            M('Member')->where("uid={$user_info['uid']}")->save($save);
            $this->success('登录成功',$data);
        } else {
            $this->error(400,'账号密码错误');
        }
    }

    /**
     * 注册
     */
    public function register()
    {

        $phone = $data['phone'] = I('phone');
        $password = I('password');
		$name=I('name');

        //匹配手机号格式是否正确
        if (!preg_match('/^1[34578]\d{9}$/', $phone)) {
            $this->error(401, '手机号码格式不正确');
        }
        //匹配密码格式是否正确
        if (preg_match('/^\S{6,18}$/', $password)) {
            $data['password'] = sp_password($password);
        } else {
            $this->error(401, '密码格式不正确');
        }

        if(M('member')->where("phone = '%s'",$phone)->count()){
            $this->error(401,'手机号码重复');
        }
        if(empty($name)){
        	$this->error(401,'姓名不能为空');
        }
		$data['password']=sp_password($password);
        $data['name']=$name;
        $data['phone']=$phone;
        //$data['create_time']=time();
	    if(M('Member')->add($data) !== false){
	    	$this->success('注册成功');
	    }else{
	    	$this->error(404,"注册失败");
	    }
    }


	/**
	 *日志
	 */
	public function log(){
		$get=I('get.');
		$post=I('post.');
		var_dump($get);
		Log::record("这是当贝get测试的日志:".json_encode($get),Log::DEBUG,true);
		Log::record("这是当贝post测试的日志:".json_encode($post),Log::DEBUG,true);
		//Log::record("这是当贝get测试的日志:".json_encode($get),Log::DEBUG,true);
		//Log::save();
		echo "success";
		exit;
	}

	public function memcache(){
		$m=new \memcached();
		$m->addServer('127.0.0.1',11211);
		//print_r($m->getStats())
		$m->add("add", 123,600);
		$m->add("add", "add2",600);//不会替换前面的数据
		$m->replace("add", "add1");//修改前面的值
		echo  $m->get("add");
		var_dump($m->get("add"));

	}
}